#include "../inc/Ui/BookmarkPlaceForm.h"
#include "../inc/Ui/WebForm.h"
#include "../inc/Utility/MyParserXml.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Base::Collection;

int BookmarkPlaceForm::FormId = -1;

BookmarkPlaceForm::BookmarkPlaceForm(void) {
}

BookmarkPlaceForm::~BookmarkPlaceForm(void) {
}

bool BookmarkPlaceForm::Initialize() {
	Form::Construct(L"BookmarkPlaceForm");

	return true;
}

result BookmarkPlaceForm::OnInitializing(void) {
	result r = E_SUCCESS;

	// TODO: Add your initialization code here
	this->SetSoftkeyActionId(SOFTKEY_0, ID_SOFTKEY_OK);
	this->SetSoftkeyActionId(SOFTKEY_1, ID_SOFTKEY_BACK);
	this->AddSoftkeyActionListener(SOFTKEY_0, *this);
	this->AddSoftkeyActionListener(SOFTKEY_1, *this);

	pEditFieldChuThich = static_cast<EditArea*> (GetControl(
			"IDC_EDITAREA_CHUTHICH"));
	pEditFieldTieuDe = static_cast<EditField*> (GetControl(
			"IDC_EDITFIELD_TIEUDE"));
	return r;
}

result BookmarkPlaceForm::OnTerminating(void) {
	result r = E_SUCCESS;

	// TODO: Add your termination code here

	return r;
}
/*
void BookmarkPlaceForm::SetParentForm(Form *pParent) {
	this->pFormParent = pParent;

}

void BookmarkPlaceForm::SetFrame(Frame *pFrame) {
	this->pFrame = pFrame;
}*/
void BookmarkPlaceForm::ThucHienLuuDuLieuVaoFile() {
	AppLog("Vao Vi tri yeu thich thuc hien luu tru");
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	Form *pWebForm = null;
	result r = pFormManager->GetForm(WebForm::GetFormId(), pWebForm);
	if (IsFailed(r)) {
		return;
	}

	MyParserXml*p = new MyParserXml();
	String a = static_cast<WebForm*> (pWebForm)->getMyLat();
	String b = static_cast<WebForm*> (pWebForm)->getMyLng();
	ArrayList* listName = new ArrayList();
	ArrayList* listValue = new ArrayList();
	listName->Add(*(new String("Lat")));
	listName->Add(*(new String("Lng")));
	listName->Add(*(new String("TieuDe")));
	listName->Add(*(new String("ChuThich")));
	listValue->Add(*(new String(a)));
	listValue->Add(*(new String(b)));
	listValue->Add(*(new String(pEditFieldTieuDe->GetText())));
	listValue->Add(*(new String(pEditFieldChuThich->GetText())));
	p->AddNewNodeWithArray("/Home/ViTriYeuThich.xml", listName, listValue,
			"ViTriYeuThich");

}
void BookmarkPlaceForm::OnActionPerformed(const Osp::Ui::Control& source,
		int actionId) {
	FormManager *pFormManager = null;
	FormManager::GetInstance(pFormManager);
	switch (actionId) {
	case ID_SOFTKEY_OK:
		AppLog("Ok");

		if (pEditFieldTieuDe->GetTextLength() == 0
				|| pEditFieldChuThich->GetTextLength() == 0) {
			MessageBox msg;
			msg.Construct("Chào bạn", "Bạn chưa nhập tiêu đề", MSGBOX_STYLE_OK);
			int i = 0;
			msg.ShowAndWait(i);
			return;
		} else {
			ThucHienLuuDuLieuVaoFile();
			pEditFieldTieuDe->SetText("");
			pEditFieldChuThich->SetText("");
			/*pFrame->SetCurrentForm(*pFormParent);
			 pFormParent->RequestRedraw(true);
			 */
			AppLog("Vào chuyển form");
			pFormManager->SetCurrentForm(WebForm::GetFormId());

		}
		//Luu vao file

		break;
	case ID_SOFTKEY_BACK:
		AppLog("Back");
		pFormManager->SetCurrentForm(WebForm::GetFormId());

		break;
	}
}

